import validate from '@/common/graceChecker.js';
import settings from '@/common/sysEnum.js';
const state = {
  companyName: '',
  sessionKey: null,
  openId: null,
  userInfo: null,
  unreadQuote: null // 未读报价信息
};

const getters = {
  /**
   * 获取用户公司名称
   * @param state
   * @returns {null}
   */
  getCompanyName: (state) => {
    if (validate.stringIsNullOrEmpty(state.companyName)) {
      let userInfo = userInfo ? userInfo : uni.getStorageSync(settings.enums.USER.USER_INFO);
      state.companyName = userInfo.companyName;
    }
    return state.companyName;
  },
  /**
   * 获取货单小助手用户信息
   * @param state
   * @returns {null}
   */
  getUserInfo: (state) => {
    state.userInfo = state.userInfo ? state.userInfo : uni.getStorageSync(settings.enums.USER.USER_INFO);
    return state.userInfo;
  },
  /**
   * 获取sessionKey
   * @param state
   * @returns {null}
   */
  getSessionKey: (state) => {
    if (validate.stringIsNullOrEmpty(state.sessionKey)) {
      let sessionkey = uni.getStorageSync(settings.enums.USER.SESSION_KEY);
      let userInfo = userInfo ? userInfo : uni.getStorageSync(settings.enums.USER.USER_INFO);
      state.sessionKey = !validate.stringIsNullOrEmpty(sessionkey) ? sessionkey : userInfo.sessionKey;
    }
    return state.sessionKey;
  },
  getOpenId: (state) => {
    if (validate.stringIsNullOrEmpty(state.openId)) {
      let userInfo = userInfo ? userInfo : uni.getStorageSync(settings.enums.USER.USER_INFO);
      state.openId = userInfo.openId;
    }
    return state.openId;
  },
  getUnreaduote: (state) => {
    if (state.unreadQuote == undefined) {
      state.unreadQuote =  uni.getStorageSync(settings.enums.USER.UNREAD_QUOTE);
    }
    return state.unreadQuote;
  }
};

const mutations = {
  /**
   * 缓存用户信息数据
   * @param state
   * @param userInfo 用户信息数据
     */
  updateUserInfo (state, userInfo) {
    state.userInfo = userInfo;
    uni.setStorageSync(settings.enums.USER.USER_INFO, userInfo); // 缓存到本地存储空间
  },
  /**
   * 缓存用户SessionKey
   * @param state
   * @param sessionKey 用户登录微信的session_key
     */
  updateSessionKey (state, sessionKey) {
    state.sessionKey = sessionKey;
    uni.setStorageSync(settings.enums.USER.SESSION_KEY, sessionKey); // 缓存到本地存储空间
  },
  /**
   * 缓存用户openId
   * @param state
   * @param openId 用户微信openId
     */
  updateOpenId (state, openId) {
    state.openId = openId;
    uni.setStorageSync(settings.enums.USER.OPEN_ID, openId); // 缓存到本地存储空间
  },
  /**
   * 缓存未读报价信息
   * @param state
   * @param unreadQuote 是否有未读报价信息
     */
  updateunReadQuote (state, unreadQuote) {
    state.unreadQuote = unreadQuote;
    uni.setStorageSync(settings.enums.USER.UNREAD_QUOTE, unreadQuote); // 缓存到本地存储空间
  },
  deleteUserInfo (state) {
    uni.setStorageSync(settings.enums.USER.USER_INFO, null);
    state.companyName = null;
    state.userInfo = null;
    state.sessionKey = null;
    state.openId = null;

  }
};

const actions = {
  update_user_info ({commit}, userInfo) {
    commit('updateUserInfo', userInfo);
  },
  update_session_key ({commit}, sessionKey) {
    commit('updateSessionKey', sessionKey);
  },
  update_open_id ({commit}, openId) {
    commit('updateOpenId', openId);
  },
	update_company_name ({commit}, companyName) {
		commit('updateCompanyName', companyName);
	},
  update_unread_quote ({commit}, unreadQuote) {
		commit('updateunReadQuote', unreadQuote);
	},
  delete_user_info ({commit}) {
    commit('deleteUserInfo');
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
